//登录选项卡
$('.ull > li').click(function(){
    $('.smallbox li')
    .removeClass('active')
    .removeClass('xianshi')
    .parents('.smallbox')
    .find(`.oll >li:eq(${$(this).index()})`)
    .addClass('xianshi')
    .parents('.smallbox')
    .find(`.ull >li:eq(${$(this).index()})`)
    .addClass('active')
})


//登录
const nameInp = document.querySelector('.username')
const passInp = document.querySelector('.password')
const form = document.querySelector('form')


form.addEventListener('submit',e=>{
    e = e || window.event
    try{e.preventDefault()}catch(err){e.returnValue = false}
    const username = nameInp.value
    const password = passInp.value


    if(!username||!password){
        return layer.alert('请完整填写用户名或者密码', {
            skin: 'layui-layer-lan'
            ,closeBtn: 0
            ,anim: 1//动画类型
          });
    }
    ajax({
        url:'http://localhost:8888/users/login',
        method:'post',
        data:`username=${username}&password=${password}`,
        dataType:'json',
        success(res){
            if(res.code === 1){
                window.localStorage.setItem('token',res.token)
                window.localStorage.setItem('id',res.user.id)
                layer.confirm('登录成功，您是否跳转首页？', {
                    btn: ['确定','留在当前页面'] //按钮
                  }, function(){
                    window.location.href = './index.html'
                  }, function(){
                  });
            }
            if(res.code !== 1){
                layer.alert('账号密码错误,验证后重新登陆', {
                    skin: 'aqua '
                    ,closeBtn: 0
                    ,anim: 2 //动画类型
                  });
            }
        }
    })

   
})